<script>
// 客户/供应商模态表单页
Vue.component('store-form', {
    template: `
<el-dialog :visible.sync="dialogShow" width="600px" @close="closeModal">
    <h3 slot="title">
        <span v-if="storeForm.id">编辑</span>
        <span v-else>新增</span>
    </h3>
    <el-form ref="form" :rules="storeRules" :model="storeForm" label-width="80px">
    <el-form-item v-if="storeForm.id" class="hidden">
    <el-input v-model="storeForm.id" disabled></el-input>
    </el-form-item>
    <el-form-item prop="title" required>
    <span slot="label">{{storeType == 1 ? "客户" : "供应商" }}</span>
    <el-input autocomplete="off" v-model="storeForm.title"></el-input>
    </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
    <el-button @click="closeModal">取 消</el-button>
    <el-button type="primary" @click="handleStoreForm">确 定</el-button>
    </div>
</el-dialog>
    `,
    props: ['dialog-show','form-row','store-type'],
    computed: {
        currentTallyId() {
            return this.$store.state.currentTallyId
        },
        currentTally () {
            return this.$store.state.currentTally
        }
    },
    watch: {
        dialogShow:{
            handler (newVal) {},
            deep:true,
            immediate: true
        },
        formRow: {
            handler (newVal) {
                this.storeForm = newVal
            },
            deep:true,
            immediate: true
        }
    },
    data:function(){
        return {
            storeForm:{},
            storeRules:{
                title: [
                    { required: true, message: '请输入客户/供应商名称', trigger: 'blur' }
                ],
            },
        }
    },
    methods:{
        closeModal(){
            this.$emit('modalClose')
        },
        handleStoreForm(){
            let that = this
            let postUrl;
            if(that.storeForm.id) {
                postUrl = '/api/store/edit'
            }else{
                postUrl = '/api/store/add'
            }
            that.storeForm.tally_id = that.currentTallyId
            that.storeForm.type = that.storeType
            axios.post(postUrl,that.storeForm,{
                headers: {'token': loginToken},
            }).then(function(res) {
                if(res.data.status === 'success'){
                    that.$message({
                        type: 'success',
                        message: '数据保存成功'
                    });
                    that.$store.dispatch('getStoreList', {
                        tallyId: that.currentTallyId,
                        type: that.storeType
                    })
                    that.$emit('modalClose')
                }else{
                    that.$message({
                        type: 'error',
                        message: '解析数据失败:'+res.data.error_message
                    })
                }
            })
        },
    }
})
</script>
